﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    class GDELIVERYReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        byte[] b; 
        string ARTICLE;
        string FACTORY;
        string WBBDPART;
        string DATE;
        string ORDERNO;
        string VKAUFNum = "";
        string WBBTAX;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string orderComments = "备注：";
        string strExcelFile;
        public string Customer = "";
        public string Address = "";
        string DateCurrent = "";
        public string PrintType = "JustExcel";
        public ArrayList listVV06, ListColor, listBatchNo;
        #endregion
        public GDELIVERYReport()
            : base()
        {
        }
        public GDELIVERYReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            ARTICLE = dicParamater["ARTICLE"].ToString().Trim();
            FACTORY = dicParamater["FACTORY"].ToString().Trim();
            WBBDPART = dicParamater["WBBDPART"].ToString().Trim();
            DocType = dicParamater["DocType"].ToString().Trim();
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            DateCurrent = DateTime.Now.Year.ToString() +"年"+ DateTime.Now.Month.ToString() +"月"+ DateTime.Now.Day.ToString()+"日";
            DATE = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            ORDERNO = DATE + DateTime.Now.ToShortTimeString().Replace(@":", "");
                    
        }
        #region------------------------------Function------------------------ 
        private void FillInWBBST(DataTable dtSource)
        {
            try
            {
                string WBBSTKNR = "";
                string WEI = "";
                //string WBBDPART = "";
                int x=0,startx = 10;
                int y = 1;
                int page = 0,runs=0;
                int rowsperpage=26;
                int pagestart = 11;                
                x = startx;
                //pagestart = (page) * rowsperpage + startx;                
                bool flgBreak=true;
                decimal qty = 0;
               #region FillExcel

                foreach (DataRow dwSource in dtSource.Rows)
                {
                            
                            //WBBDPART = dwSource["WBBDPART"].ToString().Trim();
                            WBBSTKNR = dwSource["WBBSTKNR"].ToString().Trim();
                            WEI = dwSource["MTR"].ToString();
                            qty = System.Math.Round(System.Convert.ToDecimal(WEI), 1);
                            //subtotalqty += qty;
                            
                            x += 1;
                            if (x - pagestart == 10)
                            {
                                
                                x = pagestart;
                                y += 7;
                                runs++;
                              
                                if (runs == 4)
                                {
                                    runs = 0;
                                    page++;
                                    x = (page) * rowsperpage + startx + 1;
                                    pagestart = x;
                                    y = 1;
                                    //int i = 5;
                                    exAgent.SetCellValue(x - 3, y , FACTORY);
                                    exAgent.SetCellValue(x - 3, y + 5, ARTICLE);
                                    exAgent.SetCellValue(x - 3, y + 10, WBBDPART);
                                    exAgent.SetCellValue(x - 6, y + 22, DateCurrent);
                                    exAgent.SetCellValue(x - 7, y + 22, ORDERNO);
                                }
                               
                            }
                            if (runs == 0 && flgBreak)
                            {

                                exAgent.SetCellValue(x - 3, y, FACTORY);
                                exAgent.SetCellValue(x - 3, y + 5, ARTICLE);
                                exAgent.SetCellValue(x - 3, y + 10, WBBDPART);
                                exAgent.SetCellValue(x - 6, y + 22, DateCurrent);
                                exAgent.SetCellValue(x - 7, y + 22, ORDERNO);
                                flgBreak = false;

                            }
                            
                            exAgent.SetCellValue(x, y, WBBSTKNR);
                            exAgent.SetCellValue(x, y + 5, qty);
                            
                        }
                        #endregion
                        
                            
                        
                        flgBreak = true;
                  
                HideNullRows(page); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); 
            }
        }
        private void HideNullRows(int pages)
        {
            try
            {
                exAgent.HideRows((pages+1)*26+1, 1, 200, 999);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        public void GetGDELIVERYReport(string fileType)
        {

            var query = from dwWBBST in dsSource.Tables["WBBST"].AsEnumerable()
                        select dwWBBST;

            string strSheetName;
            string TableName = "";
            int Rown = 0;
            string sRow = "";
            Queue CellQueue = new Queue();
            string value = "";
            string filename = "";
            string sheetName = "";
            string Standard = "GDELIVERY";
            switch (fileType)
            {
                case "EXCEL":

                    string filePath = @"C:\intex.w\Report\" + strExcelFile;
                    filePath =   strExcelFile;
                    exAgent.Open(filePath);
                    exAgent.Company = strCompany;
                    exAgent.strExcelFile = strExcelFile;
                    if (true)
                    {
                        filename = @"C:\intex.w\Report\发货单\" + FACTORY + DATE + ".xls";
                        sheetName = ARTICLE + "-"+ WBBDPART +"-"+ DateTime.Now.ToShortTimeString().Replace(@":", "");
                        exAgent.CreateNewSheet(filename, Standard, sheetName);
                        FillInWBBST(dsSource.Tables["WBBST"]);
                    }

                    break;
                default:
                    break;
            }
            if (exAgent != null)
            {
                if (PrintType == "DirectPrint")
                {
                    exAgent.PrintOut("Shee1");
                }
                exAgent.Save();
                exAgent.Close();
                if (PrintType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
        }
    }
}
